package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.GpmSystemConst;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmProjectReviewEntity;
import com.topscomm.gpm.service.impl.GpmProjectReviewService;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description TODO
 * @Classname SdProjectReviewScript
 * @Date 2021/12/30
 * @Created by jiangtao
 */
public class GpmProjectReviewScript extends GpmScript {
	@Transactional
	public void projectReviewAfterApproval(ITapService service, TapEntity entity) throws Exception {
		//审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceId = ConvertUtil.convertToString(nodeins.getSourceid());
		GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
		GpmProjectReviewService gpmProjectReviewService = (GpmProjectReviewService) SpringUtil.getBean("gpmProjectReviewService");
		//审批结束后，更新项目中的结项金额，项目状态，项目实际结项时间
		GpmProjectReviewEntity gpmProjectReviewEntity = gpmProjectReviewService.queryById(sourceId);
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("projectcode = '{0}'", gpmProjectReviewEntity.getProjectcode());
		List<GpmProjectEntity> sdProjectEntities = gpmProjectService.queryByWhere(sql.toString());
		if(sdProjectEntities.size()==0) {
			return;
		}
		GpmProjectEntity gpmProjectEntity = sdProjectEntities.get(0);
		//正常结项
		gpmProjectEntity.setEndtype("0");
		gpmProjectEntity.setProjectstate(GpmSystemConst.SdProjectState.ProjectNormalEnd);
		gpmProjectEntity.setActualenddate(gpmProjectReviewEntity.getEnddate());
		gpmProjectEntity.setAchievements(gpmProjectReviewEntity.getAchievements());
		gpmProjectService.updateEntity(gpmProjectEntity);
	}
}
